package com.saifan.wyy_ov.ui.communitycenter;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.saifan.wyy_ov.R;
import com.saifan.wyy_ov.data.bean.PayRecordBean;
import com.saifan.wyy_ov.data.bean.PayRecordDetailBean;
import com.saifan.wyy_ov.config.Api;
import com.saifan.wyy_ov.model.impl.GetData;
import com.saifan.wyy_ov.model.interfaces.ResponseData;
import com.saifan.wyy_ov.ui.view.BaseActivity;
import com.saifan.wyy_ov.ui.view.RefreshLayout;
import com.saifan.wyy_ov.utils.CommonAdapter;
import com.saifan.wyy_ov.utils.PriceUtils;
import com.saifan.wyy_ov.utils.StringUtil;
import com.saifan.wyy_ov.utils.ViewHolderHelper;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * @version V1.0.0
 * @author: lomasky
 * @date: 2015-5-9 14:46:31
 * @descrption 缴费记录详情
 */

public class PaythefeesInfoActivity extends BaseActivity {

    private Toolbar toolbar;
    private ListView listView;
    private TextView WYPZSKSJ;
    private TextView WYPZSKBH;
    private TextView WYPZSKJE;
    private TextView WYPZSKBS;
    private TextView WYPZSKLX;
    PayRecordBean payRecordBean;
    List<PayRecordDetailBean> beans = new ArrayList<>();
    private com.saifan.wyy_ov.ui.view.RefreshLayout refreshLayout;

    private GetData getData;
    private CommonAdapter<PayRecordDetailBean> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        payRecordBean = (PayRecordBean) getIntent().getSerializableExtra("data");
        getData = new GetData();
        setContentView();
        initAdapter();
        query();


    }

    private void initAdapter() {
        adapter = new CommonAdapter<PayRecordDetailBean>(this, beans, R.layout.fees_item) {
            @Override
            public void convert(ViewHolderHelper helper, PayRecordDetailBean item) {
                if (!StringUtil.isNullOrEmpty(item.getWYSFMX_FYNY())) {
                    item.setWYSFMX_FYNY(item.getWYSFMX_FYNY().replace("T", " "));
                }
                if (!StringUtil.isNullOrEmpty(item.getWYSFMX_GXSJ())) {
                    item.setWYSFMX_GXSJ(item.getWYSFMX_GXSJ().replace("T", " "));
                }
                helper.setText(R.id.WYSFMX_FYNY, StringUtil.returnShow(item.getWYSFMX_FYNY()));
                helper.setText(R.id.WYSFMX_FYMC, StringUtil.returnShow(item.getWYSFMX_FYMC()));
                helper.setText(R.id.WYSFMX_SKJE, getString(R.string.rmb,PriceUtils.getPrice(item.getWYSFMX_SKJE()))  );
                helper.setText(R.id.WYSFMX_SKZLJ, getString(R.string.rmb,PriceUtils.getPrice(item.getWYSFMX_SKZLJ()))  );
                helper.setText(R.id.WYSFMX_HJ,  getString(R.string.rmb, PriceUtils.getPrice(item.getWYSFMX_HJ())));
//                helper.setText(R.id.WYSFMX_GXSJ, StringUtil.returnShow(item.getWYSFMX_GXSJ()));


            }
        };
        listView.setAdapter(adapter);
        refreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

                refreshLayout.setRefreshing(true);
                query();
            }
        });


    }

    public void query() {
        if (payRecordBean == null) return;
        try {
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("WYSFMX_SKPZWJ", payRecordBean.getWYPZ_ZJ());


            getData.getDataJson(this, Api.Cost.MY_PAY_COST_INFO_DETAIL, jsonObject, getString(R.string.wait), new ResponseData() {

                @Override
                public void onSuccess(String json) {

                    List<PayRecordDetailBean> list = new Gson().fromJson(json, new TypeToken<List<PayRecordDetailBean>>() {
                    }.getType());
                    if (list!=null&&!list.isEmpty()) {
                        beans = list;
                        adapter.mDatas = beans;
                        adapter.notifyDataSetChanged();
                    }

                    refreshLayout.setRefreshing(false);
                }

                @Override
                public void onFailed(String str) {

                    refreshLayout.setRefreshing(false);
                }
            });
        } catch (JSONException e) {
        }
    }

    public void setContentView() {
        setContentView(R.layout.activity_paythefees_info);
        this.refreshLayout = (RefreshLayout) findViewById(R.id.refreshLayout);
        this.listView = (ListView) findViewById(R.id.listView);

        this.toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        View headerView = getLayoutInflater().inflate(R.layout.paythefeesinfo_header, null);
        this.WYPZSKLX = (TextView) headerView.findViewById(R.id.WYPZ_SKLX);
        this.WYPZSKBS = (TextView) headerView.findViewById(R.id.WYPZ_SKBS);
        this.WYPZSKJE = (TextView) headerView.findViewById(R.id.WYPZ_SKJE);
        this.WYPZSKBH = (TextView) headerView.findViewById(R.id.WYPZ_SKBH);
        this.WYPZSKSJ = (TextView) headerView.findViewById(R.id.WYPZ_SKSJ);

        listView.addHeaderView(headerView);

        if (payRecordBean != null) {
            WYPZSKBS.setText(payRecordBean.getWYPZ_SKBS() + "");
            WYPZSKLX.setText(StringUtil.returnShow(payRecordBean.getWYPZ_SKLX()));
            WYPZSKJE.setText(" " + PriceUtils.getPrice(payRecordBean.getWYPZ_SKJE()));
            WYPZSKBH.setText(StringUtil.returnShow(payRecordBean.getWYPZ_SKBH()));
            WYPZSKSJ.setText(StringUtil.returnShow(payRecordBean.getWYPZ_SKSJ()));

        }


    }


}
